// pages/study/index.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    getToday: '',//当前日期
    chosedMonth: '',
    arr: [],
    sysW: 100,
    marLet: '',//左边边距
    weekArr: ['日', '一', '二', '三', '四', '五', '六'],
    monthIndex: '',
    months: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12'],
    booklist: [],//状态一
    liveList: [],
    navContent: '',
    page: 1,
    pagesize: '',
    endloading: false,
    current: ''
  },
  //点击日期查看对应的课程
  dateCLass(e) {
    if (this.data.current == e.currentTarget.id) {
      this.setData({
        current: ''
      })
    } else {
      wx.showLoading({
        title: '',
      })
      this.setData({
        current: e.currentTarget.id
      })
      this.getCourse()
    }
  },
  getCourse() {
    let data = {
      method: 'order.getcoursebyday',
      section_time: this.data.year + '-' + this.data.months[this.data.monthIndex] + '-' + (this.data.current || this.data.getToday),
      token: wx.getStorageSync('token'),
    }
    app.api.post('/api/index.html', data).then((res) => {
      if (res.status) {
        wx.hideLoading()
        this.setData({
          liveList: res.data
        })
      }
    })
  },
  onLive(e) {
    wx.navigateTo({
      url: '/pages/liveDetails/index?id=' + e.currentTarget.dataset.id,
    })
  },
  teacher(e) {
    wx.navigateTo({
      url: '/pages/teacherDetails/index?id=' + e.currentTarget.dataset.id,
    })
  },
  //上个月
  toLastMon() {
    let date = this.data.chosedMonth;
    date.setMonth(date.getMonth() - 1);
    let getToday = '';
    if ((new Date()).getMonth() == (date.getMonth())) {
      getToday = (new Date()).getDate()
    }
    this.getTime(date)
    this.getDate()
    this.setData({
      chosedMonth: date,
      getToday: getToday,
    })
  },
  //下个月
  toNextMon() {
    let date = this.data.chosedMonth;
    date.setMonth(date.getMonth() + 1);
    let getToday = '';
    if ((new Date()).getMonth() == (date.getMonth())) {
      // -11
      getToday = (new Date()).getDate()
    }
    this.getTime(date)
    this.getDate()
    this.setData({
      chosedMonth: date,
      getToday: getToday,
    })
  },
  //获取日历相关参数
  dataTime: function (date) {
    //   var date = new Date('2021-7-2');
    var year = date.getFullYear();
    var month = date.getMonth();  //0（一月） 到 11（十二月） 
    var months = date.getMonth() + 1;//这个是当前的月份
    this.data.year = year //当前的年份
    this.data.monthIndex = month;//当前月份的索引
    this.data.getToday = date.getDate();//获取当前的日期 8号
    var d = new Date(year, months, 0);//Wed Jun 30 2021 00:00:00 GMT+0800 (中国标准时间)  2021年6月30日 周三
    this.data.lastDay = d.getDate();//这是当前月份的所有天数 30
    let firstDay = new Date(year, month, 1);//Tue Jun 01 2021 00:00:00 GMT+0800 (中国标准时间)
    this.data.firstDay = firstDay.getDay();// 第一天开始前的位置
  },
  getTime(date) {
    this.dataTime(date);
    //先清空数组，根据得到今月的最后一天日期遍历 得到所有日期
    if (this.data.arr) {
      this.data.arr = [];
    }
    for (var i = 0; i < this.data.lastDay; i++) {
      var obj = {};
      obj.day = i + 1;
      obj.isbook = 0//无状态
      obj.status = false
      this.data.arr.push(obj);
      // this.data.booklist.forEach(item => {
      //   if (this.data.arr[i].day == item) {
      //     this.data.arr[i].isbook = 1//状态一
      //   }
      // })
      // if (this.data.arr[i].day == this.data.getToday) {
      //   this.data.arr[i].isbook = 1//状态一
      // }
    }
    this.setData({
      marLet: this.data.firstDay,
      arr: this.data.arr,
      monthIndex: this.data.monthIndex,
      getToday: this.data.getToday,
      year: this.data.year
    });
  },
  liveDetails(e) {
    let idx = e.currentTarget.id
    let type = this.data.navContent[idx].type
    let id = this.data.navContent[idx].id
    let pinAllow = this.data.navContent[idx].collage_allow
    if (type == 1 && pinAllow == 0) {
      wx.navigateTo({
        url: '/pages/liveDetails/index?id=' + id,
      })
    } else {
      wx.navigateTo({
        url: '/pages/livePt/index?id=' + id,
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getLive()
  },
  getDate() {
    let data = {
      method: 'order.getcourseday',
      month: this.data.year + '-' + this.data.months[this.data.monthIndex],
      token: wx.getStorageSync('token'),
    }
    app.api.post('/api/index.html', data).then((res) => {
      if (res.status) {
        for (let i = 1; i < this.data.lastDay; i++) {
          res.data.forEach(item => {
            if (this.data.arr[i].day == item) {
              this.data.arr[i].status = true
            }
          })
        }
        this.setData({
          arr: this.data.arr
        })
      }
    })
  },
  // 直播推广
  getLive() {
    let data = {
      method: 'course.getlist',
      cate_id: '',
      page: this.data.page,
      type: '1'
    }
    app.api.post('/api/index.html', data).then((res) => {
      if (res.status) {
        this.setData({
          pagesize: res.data.last_page
        })
        var datas = this.data.navContent
        if (this.data.page == 1) {
          datas = []
        }
        if (res.data.list.lenght == 0) {
          this.setData({
            endloading: true, //上拉不在加载
          })
        } else {
          this.setData({
            page: this.data.page + 1 //更新page 请求下一页数据
          })
          this.setData({
            navContent: datas.concat(res.data.list),
          })
        }
      } else {
        wx.showToast({
          title: res.msg,
          icon: 'none'
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var date = new Date();
    this.getTime(date);//获取时间
    this.setData({
      chosedMonth: date,
    })
    this.getDate()
    this.getCourse()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading()
    wx.setBackgroundTextStyle({
      textStyle: 'dark',// 下拉背景字体、loading 图的样式为dark
    })
    wx.setBackgroundColor({
      backgroundColor: '#666666', // 窗口的背景色为白色
    })
    this.setData({
      page: 1
    })
    setTimeout(() => {
      this.getLive()
      this.getDate()
      this.getCourse()
      wx.stopPullDownRefresh()
      wx.hideNavigationBarLoading()
    }, 1000);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    if (this.data.page <= this.data.pagesize) {
      wx.showLoading({
        title: '加载中'
      })
      setTimeout(() => {
        wx.hideLoading({})
        that.getLive()  //页面上拉调用这个方法
      }, 1000)
    } else {
      wx.showToast({
        title: '我已经到底了',
        icon: 'none'
      })
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})